﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogicLayer;

namespace PresentationLayer
{
    public partial class frmLanguages : Form
    {
        #region Properties
        private frmMain MdiForm { get; set; }
        private enumState State { get; set; }
        enum enumState
        {
            View,
            New,
            Edit
        }
        #endregion

        #region Constructors
        public frmLanguages(frmMain pMdiForm)
        {
            InitializeComponent();
            this.MdiForm = pMdiForm;
        }
        #endregion

        private void frmLanguages_Load(object sender, EventArgs e)
        {
            this.MdiForm.Event_OnTranslationChanged += new frmMain.OnTranslationChangedHandler(Translate);
            Translate();
            FillListViewLanguages();
            SetButtonsToState(enumState.View);
            SetTextBoxToState(enumState.View);
        }

        #region Events
        private void lsvLanguages_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetButtonsToState(enumState.View);
            SetTextBoxToState(enumState.View);
            FillFieldsWithLanguageDetails();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            SetButtonsToState(enumState.View);
            SetTextBoxToState(enumState.View);
            FillFieldsWithLanguageDetails();
            lsvLanguages.Focus();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            SetButtonsToState(enumState.New);
            SetTextBoxToState(enumState.New);
            txtName.Focus();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            SetButtonsToState(enumState.Edit);
            SetTextBoxToState(enumState.Edit);
            txtName.Focus();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }
        #endregion

        #region Public Methods
        #endregion

        #region Private Methods
        /// <summary>
        /// Saves the language.
        /// </summary>
        private void Save()
        {
            if (SaveLanguage())
            {
                FillListViewLanguages();
                SetButtonsToState(enumState.View);
                SetTextBoxToState(enumState.View);
                lsvLanguages.Focus();
            }
        }

        /// <summary>
        /// Saves the language depending on the actual state.
        /// </summary>
        /// <returns>State of success.</returns>
        private Boolean SaveLanguage()
        {
            Boolean Success = false;
            if (CheckIfTextBoxesEmpty() == false)
            {
                switch (this.State)
                {
                    case enumState.New:
                        if (this.MdiForm.DataAccess.LanguageExists(txtName.Text) == false)
                        {
                            Success = this.MdiForm.DataAccess.InsertLanguage(txtName.Text);
                        }
                        else
                        {
                            MessageBox.Show("The language does allready exist.");
                        }
                        break;
                    case enumState.Edit:
                        Success = this.MdiForm.DataAccess.ChangeLanguage(GetLanguageID(), txtName.Text);
                        break;
                }
            }
            if (Success == false)
            {
                MessageBox.Show("Error saving language.");
            }
            return Success;
        }

        /// <summary>
        /// Gets the actually selected language ID.
        /// </summary>
        /// <returns>The ID of the language.</returns>
        private Int32 GetLanguageID()
        {
            if (lsvLanguages.SelectedItems.Count > 0)
            {
                clsLanguage Language = (clsLanguage)lsvLanguages.SelectedItems[0].Tag;
                return Language.ID;
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// Checks the emptyness of the TextBoxes.
        /// </summary>
        /// <returns>True if TextBoxes are empty.</returns>
        private Boolean CheckIfTextBoxesEmpty()
        {
            if (txtName.Text == "")
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Translates every visible element.
        /// </summary>
        private void Translate()
        {
            this.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, "Text", this.Text, 0);
            grpLanguages.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, grpLanguages.Name, grpLanguages.Text, 0);
            grpDetails.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, grpDetails.Name, grpDetails.Text, 0);
            lblName.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblName.Name, lblName.Text, 0);
            lblCreated.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblCreated.Name, lblCreated.Text, 0);
            lblChanged.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblChanged.Name, lblChanged.Text, 0);
            grpOptions.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, grpOptions.Name, grpOptions.Text, 0);
            btnCancel.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, btnCancel.Name, btnCancel.Text, 0);
            btnNew.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, btnNew.Name, btnNew.Text, 0);
            btnEdit.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, btnEdit.Name, btnEdit.Text, 0);
            btnSave.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, btnSave.Name, btnSave.Text, 0);
            lblCreatedBy.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblCreatedBy.Name, lblCreatedBy.Text, 0);
            lblChangedBy.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblChangedBy.Name, lblChangedBy.Text, 0);
            InitializeListViewLanguages();
        }

        /// <summary>
        /// Initializes the languages ListView.
        /// </summary>
        private void InitializeListViewLanguages()
        {
            if (lsvLanguages.Columns.Count > 0)
            {
                lsvLanguages.Columns.Clear();
            }
            lsvLanguages.Columns.Add(this.MdiForm.DataAccess.GetTranslation(this.Name, lsvLanguages.Name + "_Column1_Language", "Language", 0));
            lsvLanguages.View = View.Details;
            lsvLanguages.FullRowSelect = true;
            lsvLanguages.HideSelection = false;
            lsvLanguages.MultiSelect = false;
        }

        /// <summary>
        /// Fills the ListView with users.
        /// </summary>
        private void FillListViewLanguages()
        {
            //Delete all ListView items if available.
            if (lsvLanguages.Items.Count > 0)
            {
                lsvLanguages.Items.Clear();
            }
            //Get all languages.
            ArrayList Languages = this.MdiForm.DataAccess.GetAllLanguages();
            for (Int32 i = 0; i < Languages.Count; i++)
            {
                //Get language.
                clsLanguage Language = (clsLanguage)Languages[i];
                //Create item for ListView.
                ListViewItem Item = new ListViewItem(Language.Language);
                Item.Tag = Language;
                //Add item to ListView.
                lsvLanguages.Items.Add(Item);
            }
            //Resize columns adapted to contents.
            lsvLanguages.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
            //Select the first item if possible.
            if (lsvLanguages.Items.Count > 0)
            {
                lsvLanguages.Items[0].Selected = true;
            }
            else
            {
                FillFieldsWithLanguageDetails();
            }
        }

        /// <summary>
        /// Get the language information from the ListView and fill the details into the fields.
        /// </summary>
        private void FillFieldsWithLanguageDetails()
        {
            if (lsvLanguages.SelectedItems.Count > 0)
            {
                clsLanguage Language = (clsLanguage)lsvLanguages.SelectedItems[0].Tag;
                lblDateCreatedVal.Text = Language.DateCreated.ToString();
                lblDateChangedVal.Text = Language.DateChanged.ToString();
                lblUserCreatedVal.Text = Language.UserCreated;
                lblUserChangedVal.Text = Language.UserChanged;
                lblCreatedBy.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblCreatedBy.Name, lblCreatedBy.Text, 0);
                lblChangedBy.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblChangedBy.Name, lblChangedBy.Text, 0);
                txtName.Text = Language.Language;
            }
            else
            {
                lblDateCreatedVal.Text = "";
                lblDateChangedVal.Text = "";
                lblUserCreatedVal.Text = "";
                lblUserChangedVal.Text = "";
                lblCreatedBy.Text = "";
                lblChangedBy.Text = "";
                txtName.Text = "";
            }
        }

        /// <summary>
        /// Applies the state to the buttons.
        /// </summary>
        /// <param name="pState">The state.</param>
        private void SetButtonsToState(enumState pState)
        {
            this.State = pState;
            switch (this.State)
            {
                case enumState.View:
                    btnCancel.Enabled = false;
                    btnNew.Enabled = true;
                    btnSave.Enabled = false;
                    if (lsvLanguages.SelectedItems.Count > 0)
                    {
                        if (lsvLanguages.SelectedItems[0].Index > -1)
                        {
                            btnEdit.Enabled = true;
                        }
                        else
                        {
                            btnEdit.Enabled = false;
                        }
                    }
                    else
                    {
                        btnEdit.Enabled = false;
                    }
                    break;
                case enumState.New:
                    btnCancel.Enabled = true;
                    btnNew.Enabled = false;
                    btnEdit.Enabled = false;
                    btnSave.Enabled = true;
                    break;
                case enumState.Edit:
                    btnCancel.Enabled = true;
                    btnNew.Enabled = false;
                    btnEdit.Enabled = false;
                    btnSave.Enabled = true;
                    break;
            }
        }

        /// <summary>
        /// Applies the state to the TextBoxes.
        /// </summary>
        /// <param name="pState">The state.</param>
        private void SetTextBoxToState(enumState pState)
        {
            this.State = pState;
            switch (this.State)
            {
                case enumState.View:
                    txtName.Enabled = false;
                    lsvLanguages.Enabled = true;
                    break;
                case enumState.New:
                    txtName.Enabled = true;
                    lsvLanguages.Enabled = false;
                    txtName.Text = "";
                    lblDateCreatedVal.Text = "";
                    lblDateChangedVal.Text = "";
                    lblUserCreatedVal.Text = "";
                    lblUserChangedVal.Text = "";
                    lblCreatedBy.Text = "";
                    lblChangedBy.Text = "";
                    break;
                case enumState.Edit:
                    txtName.Enabled = true;
                    lsvLanguages.Enabled = false;
                    break;
            }
        }
        #endregion
    }
}
